<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="aeId" prop="aeProductId">
        <el-input
          v-model="queryParams.aeProductId"
          placeholder="请输入aeId"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺id" prop="storeId">
        <el-select v-model="queryParams.storeId" placeholder="请选择" clearable filterable>
          <el-option v-for="dict in storeList" :key="dict.value" :label="dict.lable" :value="dict.value"/>
        </el-select>
        <!--        <el-input-->
        <!--          v-model="queryParams.storeId"-->
        <!--          placeholder="请输入店铺id"-->
        <!--          clearable-->
        <!--          @keyup.enter.native="handleQuery"-->
        <!--        />-->
      </el-form-item>
      <el-form-item label="任务状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择任务状态" clearable>
          <el-option v-for="dict in dict.type.task_status" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="错误原因" prop="remark">
        <el-input v-model="queryParams.remark" placeholder="请输入错误原因" clearable/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="handleAdd('1')">通过AE新增</el-button>
        <el-button type="success" plain size="mini" @click="handleAdd('2')">通过店铺新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['gather:aeProductDiscountAdjustmentTask:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="aeProductDiscountAdjustmentTaskList" @selection-change="handleSelectionChange"
              border stripe>
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="店铺名称" align="center" prop="storeName"/>
      <el-table-column label="aeId" align="center" prop="aeProductId"/>
      <el-table-column label="在线商品id" align="center" prop="sellerStoreProductId"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180"/>
      <el-table-column label="折扣状态" align="center" prop="activityStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.activity_status" :value="scope.row.activityStatus"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="折扣失败原因" align="center" prop="activityRemark">
        <template slot-scope="scope">
          <div>{{ scope.row.activityRemark ? scope.row.activityRemark : '——' }}</div>
        </template>
      </el-table-column>
      <el-table-column label="调价状态" align="center" prop="editStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.price_adjustment_status" :value="scope.row.editStatus"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="调价失败原因" align="center" prop="editRemark">
        <template slot-scope="scope">
          <div>{{ scope.row.editRemark ? scope.row.editRemark : '——' }}</div>
        </template>
      </el-table-column>
      <el-table-column label="任务状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_status" :value="scope.row.status"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="错误原因" align="center" prop="remark">
        <template slot-scope="scope">
          <div>{{ scope.row.remark ? scope.row.remark : '——' }}</div>
        </template>
      </el-table-column>
      <el-table-column label="新折扣" align="center" prop="newDiscount">
        <template slot-scope="scope">
          <div>{{ scope.row.newDiscount ? scope.row.newDiscount : '——' }}</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleRetest(scope.row)">手动测试</el-button>
          <el-button size="mini" type="text" @click="handleRollback(scope.row)">数据回滚</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
          <el-button size="mini" type="text" @click="getProductStatus(scope.row)">查看上架状态</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改速卖通商品价格调整任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="aeId" prop="aeProductIds" v-if="type== '1'">
          <el-input v-model="form.aeProductIds" placeholder="请输入aeId"/>
        </el-form-item>
        <el-form-item label="店铺" prop="storeName" v-if="type== '2'">
          <el-input v-model="form.storeName" placeholder="请输入店铺名称"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAeProductDiscountAdjustmentTask,
  getAeProductDiscountAdjustmentTask,
  delAeProductDiscountAdjustmentTask,
  addAeProductDiscountAdjustmentTask,
  anewDisposeTask, dataRollback, selCollectProductStatus
} from "@/api/priceAdjustment/priceAdjustmentTask";
import {queryUserShopList} from "@/api/shop";

export default {
  name: "AeProductDiscountAdjustmentTask",
  dicts: ['price_adjustment_status', 'task_status', 'activity_status'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 速卖通商品价格调整任务表格数据
      aeProductDiscountAdjustmentTaskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        aeProductId: undefined,
        sellerStoreProductId: undefined,
        storeId: undefined,
        remark: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        aeProductIds: [
          {required: true, message: "aeId不能为空", trigger: "blur"}
        ],
        storeName: [
          {required: true, message: "店铺id不能为空", trigger: "blur"}
        ]
      },
      storeList: [],
      type: "",//新增类型
    };
  },
  created() {
    this.getList();
    this.getEmpowerList()
  },
  methods: {
    // 获取店铺账号选择列表
    getEmpowerList() {
      this.loading = true;
      queryUserShopList().then(res => {
        this.storeList = res.data;
        this.loading = false;
      });
    },
    /** 查询速卖通商品价格调整任务列表 */
    getList() {
      this.loading = true;
      listAeProductDiscountAdjustmentTask(this.queryParams).then(response => {
        this.aeProductDiscountAdjustmentTaskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        aeProductIds: undefined,
        sellerStoreProductId: undefined,
        storeName: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(type) {
      this.reset();
      this.type = type
      this.title = type == '1' ? '通过AE新增任务' : '通过店铺新增任务';
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getAeProductDiscountAdjustmentTask(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改速卖通商品价格调整任务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          this.form.type = this.type
          addAeProductDiscountAdjustmentTask(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      });
    },
    // 重新测试
    handleRetest(row) {
      this.$confirm('是否确定执行重新测试操作？', '提示').then(() => {
        let data = {
          id: row.id
        }
        anewDisposeTask(data).then(response => {
          this.$modal.msgSuccess(response.msg);
          this.open = false;
          this.getList();
        }).finally(() => {
          this.buttonLoading = false;
        });
      })
    },
    // 数据回滚
    handleRollback(row) {
      this.$confirm('是否确定执行数据回滚操作？', '提示').then(() => {
        let data = {
          aeProductId: row.aeProductId
        }
        dataRollback(data).then(response => {
          this.$modal.msgSuccess(response.msg);
          this.getList();
        }).finally(() => {
          this.buttonLoading = false;
        });
      })
    },
    // 查看当前商品是否上架
    getProductStatus(row) {
      let data = {
        sellerStoreId: row.sellerStoreProductId
      }
      selCollectProductStatus(data).then(response => {
        // this.$modal.msgSuccess(response.msg);
        this.$alert(response.msg, '上架状态');
        // this.getList();
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除速卖通商品价格调整任务编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delAeProductDiscountAdjustmentTask(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gather/aeProductDiscountAdjustmentTask/export', {
        ...this.queryParams
      }, `aeProductDiscountAdjustmentTask_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
